/**
 * @file Start.c
 * @author liqiang (liqiang@hacint.com.cn)
 * @brief 
 * @version 1.0.0
 * @date 2023-08-17
 * 
 * 
 */
#include    "Start.h"
#include    "ConfigIO.H"
#include    "delay.h"


/*测试*/
 
#include "unity.h"

/**
 * @brief 为了单元测试 定义的变量
 * 
 */ 
 
 //起始端通道选4位
 typedef		struct 
{
	unsigned	char	startChannel0;	
	unsigned	char	startChannel1;
	unsigned	char	startChannel2;
	unsigned	char	startChannel3;

}StartChannel_Type;

//起始段端口选3位
typedef		struct 
{
	unsigned	char	startPort0;
	unsigned	char	startPort1;	
	unsigned	char	startPort2;

}StartPort_Type;

//起始端板选3位
typedef		struct 
{
	
	unsigned	char	startCard0;	
	unsigned	char	startCard1;
	unsigned	char	startCard2;

}StartCard_Type;

//起始端箱选4位
typedef		struct 
{
	
	unsigned	char	startBox0;	
	unsigned	char	startBox1;
	unsigned	char	startBox2;
	unsigned	char	startBox3;
}StartBox_Type;

//起始端针位信息
typedef		struct 
{
	unsigned	char	channel;
	unsigned	char	port;
	unsigned	char	card;	
	unsigned	char	box; 

}StartPin_Type;

StartBox_Type			startBox;
StartCard_Type			startCard;
StartPort_Type			startPort;
StartChannel_Type		startChannel;

StartPin_Type			spinStr;

//MS表示MCU_START   表示MCU的起始针相关的寄存器读写操作
//BIT_ADDR返回一个可以直接读写该位的引用
#define MS_CHANNEL0	 		BIT_ADDR(MS_A0_ADDR,MS_A0_BitNum)
#define MS_CHANNEL1	 		BIT_ADDR(MS_A1_ADDR,MS_A1_BitNum)
#define MS_CHANNEL2	 		BIT_ADDR(MS_A2_ADDR,MS_A2_BitNum)
#define MS_CHANNEL3	 		BIT_ADDR(MS_A3_ADDR,MS_A3_BitNum)
 
#define MS_PORT0	 		BIT_ADDR(MS_A_ADDR,MS_A_BitNum)
#define MS_PORT1	 		BIT_ADDR(MS_B_ADDR,MS_B_BitNum)
#define MS_PORT2	 		BIT_ADDR(MS_C_ADDR,MS_C_BitNum)

 
#define MS_CARD0	 		BIT_ADDR(MS_A7_ADDR,MS_A7_BitNum)
#define MS_CARD1	 		BIT_ADDR(MS_A8_ADDR,MS_A8_BitNum)
#define MS_CARD2	 		BIT_ADDR(MS_A9_ADDR,MS_A9_BitNum)


#define MS_BOX0	 			BIT_ADDR(MS_A10_ADDR,MS_A10_BitNum)
#define MS_BOX1	 			BIT_ADDR(MS_A11_ADDR,MS_A11_BitNum)
#define MS_BOX2	 			BIT_ADDR(MS_A12_ADDR,MS_A12_BitNum)
#define MS_BOX3	 			BIT_ADDR(MS_A13_ADDR,MS_A13_BitNum)

/**起始端   通道选ID设置
从 chanlID 中提取位并赋值给 startChannel 结构体*/
void    GetStartChanlIDByBit(unsigned int chanlID)
{

        startChannel.startChannel0 =   (chanlID&0X01); //提取第 0 位
        startChannel.startChannel1 =   (chanlID&0X02)>>1;// 提取第 1 位
        startChannel.startChannel2 =   (chanlID&0X04)>>2;// 提取第 2 位
        startChannel.startChannel3 =   (chanlID&0X08)>>3;// 提取第 3 位
}

void    SetStartChanlNO(unsigned  int chanlID)
{		
		//读chanlID
		GetStartChanlIDByBit(chanlID);
		// 设置通道引脚状态
		MS_CHANNEL0 =	startChannel.startChannel0  ;
		MS_CHANNEL1 =	startChannel.startChannel1  ;
		MS_CHANNEL2 =	startChannel.startChannel2  ;
		MS_CHANNEL3	=	startChannel.startChannel3	;	
}

/*起始端   端口选ID设置
从 portID 中提取位并赋值给 startPort 结构体*/
void    GetStartPortIDByBit(unsigned int portID)
{

        startPort.startPort0 =   (portID&0X01);
        startPort.startPort1 =   (portID&0X02)>>1;   
        startPort.startPort2 =   (portID&0X04)>>2;
      
}
void    SetStartPortNO(unsigned int portID)
{
        GetStartPortIDByBit(portID);

		MS_PORT0   =	 startPort.startPort0;
		MS_PORT1   =	 startPort.startPort1;
		MS_PORT2   =	 startPort.startPort2;
     
}

/*起始端    板选ID设置
从 cardID 中提取位并赋值给 startCard 结构体*/
void    GetStartCardIDByBit(unsigned int cardID){

        startCard.startCard0 =   (cardID&0X01);
        startCard.startCard1 =   (cardID&0X02)>>1; 
        startCard.startCard2 =   (cardID&0X04)>>2;
       
}
void    SetStartCardNO(unsigned int cardID){
        GetStartCardIDByBit(cardID);
        MS_CARD0    =   startCard.startCard0;
        MS_CARD1    =   startCard.startCard1;
        MS_CARD2    =   startCard.startCard2;
       
}

/*起始端    箱选ID设置
从 boxID 中提取位并赋值给 startBox 结构体*/
void    GetStartBoxIDByBit(unsigned int boxID){

       startBox.startBox0 =(boxID&0X01);    //0000 0000 0000 0001
       startBox.startBox1 =(boxID&0X02)>>1; //0000 0000 0000 0010
       startBox.startBox2 =(boxID&0X04)>>2; //0000 0000 0000 0100
       startBox.startBox3 =(boxID&0X08)>>3; //0000 0000 0000 1000
}

void    SetStartBoxNO(unsigned  int chanlID)
{
        GetStartBoxIDByBit(chanlID);
        MS_BOX0 =	startBox.startBox0;
        MS_BOX1 =	startBox.startBox1;
        MS_BOX2 =	startBox.startBox2;
        MS_BOX3	=	startBox.startBox3;	
}

//箱选    板选    端选    通道选
//4		   3	   3		4			bit
//从spin 值中提取特定的位，给spinStr;
StartPin_Type   Get_Start_Pin_Struct(unsigned short  spin)
{
	//spin = 1,spinNum=0;全部置0，第一种状态，通道打到y0,端选第1个芯片；板选第1个；箱选第1个
	//spin = 2,spinNum=1;0000 0000 0000 0001，第2种状态，通道打到y1,端选第1个芯片；板选第1个；箱选第1个
	//这样依次往下计数，如果有8个机箱，16*8*8*8=就可以将起始针比如1-8192轮流
	unsigned short spinNum = spin-1; //1-based 编号转 0-based

	spinStr.box		=	(spinNum&0x3C00)>>10;//0011 1100 0000 0000  箱选取11-14位这四位并移动到最低位
	spinStr.card    =   (spinNum&0x0380)>>7; //0000 0011 1000 0000  卡选取8-10这3位的值并移动到最低位
	spinStr.port    =   (spinNum&0x0070)>>4; //0000 0000 0111 0000   端选
	spinStr.channel =   spinNum&0x000f;      //0000 0000 0000 1111	 通道选
	return  spinStr;
}

/* **********************设置起始针*********************************************************/
//传入spin所有start引脚的位，提取设置
void    SetStartPin(unsigned    short   spin)
{
		//从spin 值中提取特定的几位，给spinStr结构体spinStr.box、spinStr.card、spinStr.port、spinStr.channel;
        Get_Start_Pin_Struct(spin);
		
		//将spinStr结构体中spinStr.box的值按位提取分别对MS_BOX0、MS_BOX1、MS_BOX2等寄存器地址写值
		SetStartBoxNO(spinStr.box);
		//spinStr.card
        SetStartCardNO(spinStr.card);
		//spinStr.port
        SetStartPortNO(spinStr.port);
		//spinStr.channel
		SetStartChanlNO(spinStr.channel);
}
//设置起始针
void    Plat_SetStartPin(unsigned    short   spin){
        SetStartPin(spin);
}


void    ResetStartPin( void){
        SetStartPin(1);
}

//重置起始针
void    Plat_ResetStartPin(void ){

        ResetStartPin( );
}


//这个用途？？
void    SetLedStartPin(unsigned short   spin){
		
        SetStartPin(spin);
}
//设置led起始针
void    Plat_SetLedStartPin(unsigned short   spin)
{

        SetLedStartPin(spin);
}

//关闭起始针，（选到一个不存在的箱子，F箱子）
void   StartPinOff()
{
	SetStartBoxNO(0xF);	
}


void    Plat_SS_Disable(void){
        StartPinOff(); 
}


